Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / AHI / Examples / Low-level / 3rd party / GetAudioID / Source / GAI.S
blob1612254191405ce24c3833bca015bb1cd305d8d0
1 *************************************************
2 *                                               *
3 *                (C) Copyright 1996             *
4 *                                               *
5 *                  Tomi Blinnikka               *
6 *                                               *
7 *************************************************
8 *                                               *
9 * DISTRIBUTION:                                 *
10 *                                               *
11 * The complete and unmodified archive, as       *
12 * provided by Tomi Blinnikka, may be freely     *
13 * redistributed via any means which preserves   *
14 * its copyrights. The archive may not be sold   *
15 * for profit.                                   *
16 *                                               *
17 * Permission granted to be distributed with the *
18 * AHI user/development software.                *
19 *                                               *
20 *************************************************
21 *                                               *
22 * DISCLAIMER:                                   *
23 *                                               *
24 * GETAUDIOID IS PROVIDED "AS-IS" AND SUBJECT TO *
25 * CHANGE; NO WARRANTIES ARE MADE.  ALL USE IS   *
26 * AT YOUR OWN RISK.  NO LIABILITY OR            *
27 * RESPONSIBILITY IS ASSUMED.                    *
28 *                                               *
29 *************************************************
30 *                                               *
31 * DESCRIPTION:                                  *
32 *                                               *
33 * A program to display AHI AudioID requester    *
34 * and print selected ID in hex and decimal.     *
35 *                                               *
36 * Requires Kickstart 2.04+, ahi.device 1+.      *
37 *                                               *
38 *************************************************
39 *                                               *
40 * HISTORY:                                      *
41 *                                               *
42 * Version 0.01  29.04.1996                      *
43 *        -0.99รถ 29.04.1996                      *
44 *                                               *
45 * Basics.                                       *
46 *                                               *
47 *                                               *
48 * Version 1.00  30.04.1996                      *
49 *                                               *
50 * Works from CLI/Shell & Workbench.             *
51 *                                               *
52 * Added TITLE, POSITIVE, NEGATIVE, LFORMAT &    *
53 * PUBSCREEN options.                            *
54 *                                               *
55 * Added Locale support.                         *
56 *                                               *
57 *                                               *
58 * Version 1.01  01.05.1996                      *
59 *                                               *
60 * Doesn't set an initial AudioID anymore,       *
61 * since Martin tells my shouldn't make          *
62 * assumptions about direct IDs.                 *
63 *                                               *
64 * Recompiled with tags names from AHI beta 2.   *
65 *                                               *
66 * If no mode was selected then returns -5.      *
67 *                                               *
68 *                                               *
69 * BUGS:                                         *
70 *                                               *
71 *                                               *
72 *************************************************
74         INCLUDE "dos/dosextens.i"
75         INCLUDE "devices/ahi.i"
76         INCLUDE "libraries/locale.i"
77         INCLUDE "workbench/startup.i"
79         INCLUDE "JMPLibs.i"             ;Macros to open/close/call libs
80         INCLUDE "lvo/ahi_lib.i"         ;ahi.device offsets
82 ;XREFs needed
84         INCLUDE "XREF:2.0.xref"
85         INCLUDE "XREF:exec.xref"
86         INCLUDE "XREF:dos.xref"
87         INCLUDE "XREF:icon.xref"
90 ;DEBUG_MODE     SET     1
92 AHI_DEFMIXFREQ: EQU     30058           ;Sounds a lot better than 11kHz...
94 PROGVERSION:    macro
95                 dc.b    "1.01 (01.05.96)"
96                 endm
99 ;Use these and leave a few hundred include lines out
101 TRUE:           EQU     1
102 FALSE:          EQU     0
104 do_ToolTypes:   EQU     $36
107                 Section GAI,CODE
109 START:          push    all                     ;Save registers
111                 sub.l   a1,a1                   ;Find our task
112                 jlib    Exec,FindTask
113                 move.l  d0,a4
114                 move.l  pr_CLI(a4),d0
115                 bne     OpenDos
117                 lea.l   pr_MsgPort(a4),a0
118                 jlib    Exec,WaitPort
119                 lea.l   pr_MsgPort(a4),a0
120                 flib    Exec,GetMsg             ;Get WB message
121                 move.l  d0,WBMsg
123 OpenDos:        openlib Dos,NoDos               ;Keep at beginning
125                 moveq.l #0,d1                   ;Go to root
126                 jlib    Dos,CurrentDir
127                 move.l  d0,OrigLock             ;This is where we came from
130 ;Open locale.library
132                 openlib Locale,NoLocale
134                 sub.l   a0,a0
135                 jlib    Locale,OpenLocale
136                 move.l  d0,Locale1
138                 sub.l   a0,a0                   ;No locale, use default
139                 lea.l   CatalogName1,a1
140                 lea.l   CatalogTags,a2
141                 jlib    Locale,OpenCatalogA     ;Open our catalog for this language
142                 move.l  d0,GAICatalog
143                 beq     NoLocale
146 ;Here we set the text strings using locale support
148                 moveq.l #0,d7
149                 lea.l   CatStrings,a3           ;Def. String in A1
151 LocaleLoop:     move.l  d7,d0                   ;StringNum in D0
152                 move.l  GAICatalog,a0           ;Catalog in A0
153                 move.l  (a3),a1                 ;Default string in A1
154                 jlib    Locale,GetCatalogStr
156                 move.l  d0,(a3)
158                 add.l   #1,d7
159                 add.l   #4,a3
160                 cmp.l   #CatStrAmount,d7
161                 bne     LocaleLoop
164 ;Fix strings that aren't move.l'd
166 NoLocale:       move.l  STRAHIReqTitle1,OptTitle
167                 move.l  STRPositiveText2,OptPositive
168                 move.l  STRNegativeText2,OptNegative
171                 tst.l   WBMsg
172                 beq     CLIStart                ;Skip WB stuff if from CLI
175 ;Open CON: window where we print errors and the result.
177                 move.l  #CONName,d1
178                 move.l  #MODE_OLDFILE,d2
179                 jlib    Dos,Open
180                 move.l  d0,CONFile
181                 beq     ShutDown
182                 move.l  CONFile,_stdout
184                 openlib Icon,NoIcon
186                 move.l  WBMsg,a0
187                 move.l  sm_ArgList(a0),a1
188                 move.l  wa_Lock(a1),d1
189                 jlib    Dos,CurrentDir          ;Change to the directory where our icon is
192 ;The icon we open here can't be free'd until all the data from them has
193 ;been dealt with, i.e. until the progrma ends.
195                 move.l  WBMsg,a0
196                 move.l  sm_ArgList(a0),a1
197                 move.l  wa_Name(a1),a0
198                 jlib    Icon,GetDiskObject      ;Get icon
199                 move.l  d0,DiskObject
200                 beq     MainStart
202 DoToolType1:    move.l  d0,a4
203                 move.l  do_ToolTypes(a4),a0
204                 lea.l   DECIMALText1,a1
205                 flib    Icon,FindToolType
206                 tst.l   d0
207                 beq     DoToolType2
208                 move.l  #1,OptDecimal           ;This user wants decimal ID
210 DoToolType2:    move.l  do_ToolTypes(a4),a0
211                 lea.l   TITLEText1,a1
212                 flib    Icon,FindToolType
213                 tst.l   d0
214                 beq     DoToolType3
215                 move.l  d0,OptTitle             ;This title, please
217 DoToolType3:    move.l  do_ToolTypes(a4),a0
218                 lea.l   POSITIVEText1,a1
219                 flib    Icon,FindToolType
220                 tst.l   d0
221                 beq     DoToolType4
222                 move.l  d0,OptPositive          ;This positive gad text
224 DoToolType4:    move.l  do_ToolTypes(a4),a0
225                 lea.l   NEGATIVEText1,a1
226                 flib    Icon,FindToolType
227                 tst.l   d0
228                 beq     DoToolType5
229                 move.l  d0,OptNegative          ;This negative gad text
231 DoToolType5:    move.l  do_ToolTypes(a4),a0
232                 lea.l   LFORMATText1,a1
233                 flib    Icon,FindToolType
234                 tst.l   d0
235                 beq     DoToolType6
236                 move.l  d0,OptLFormat           ;Format with this
238 DoToolType6:    move.l  do_ToolTypes(a4),a0
239                 lea.l   PUBSCREENText1,a1
240                 flib    Icon,FindToolType
241                 tst.l   d0
242                 beq     DoToolType7
243                 move.l  d0,OptPubScreen         ;Open on this public screen
244 DoToolType7:
245                 bra     MainStart               ;Skip CLI stuff
248 CLIStart:       jlib    Dos,Output
249                 move.l  d0,_stdout
251                 move.l  #CLTemplate1,d1
252                 move.l  #CLArray1,d2
253                 moveq.l #0,d3
254                 flib    Dos,ReadArgs            ;Read options from CLI
255                 move.l  d0,RDArgs1
256                 beq     NoRDArgs
259 ;Open ahi.device
261 MainStart:      OPENAHI 1
262                 move.l  d0,_AHIBase
263                 beq     NoAHI                   ;Open failed
266 ;Let's set all the options correctly
268                 move.l  (OptTitle),ReqTitle
269                 move.l  (OptPositive),ReqPos
270                 move.l  (OptNegative),ReqNeg
271                 move.l  (OptPubScreen),ReqPubScr
274 ;Here we go and check that the user specified formatting string doesn't
275 ;contain more than two '%ld' strings.
277                 move.l  OptLFormat,d2
278                 beq     OpenAHIReq              ;Check if available
280                 bsr     CheckLegality           ;Check if valid
281                 tst.l   d2
282                 beq     BadFmt                  ;ERROR: Invalid
285 ;Allocate AHI's audio requester
287 OpenAHIReq:     lea.l   AHIReqTags1,a0
288                 jlib    AHI,AHI_AllocAudioRequestA
289                 move.l  d0,AHIReq1
290                 beq     NoAHIReq                ;Allocation failed
293 ;Show requester
295                 move.l  d0,a0
296                 lea.l   AHIReqTags2,a1
297                 flib    AHI,AHI_AudioRequestA
298                 cmp.l   #TRUE,d0
299                 bne     CheckCancel             ;Positive gad _not_ pressed
301 ;Get values from requester and set then into the formatting template
303                 move.l  AHIReq1,a0
305                 cmp.l   #AHI_INVALID_ID,ahiam_AudioID(a0)
306                 beq     CheckCancel             ;None selected
308                 lea.l   VFPrintfArgv,a1
309                 move.l  ahiam_AudioID(a0),0(a1)
310                 move.l  ahiam_MixFreq(a0),4(a1)
312                 move.l  #FmtString1,d2          ;Default formatting string
314                 tst.l   OptDecimal              ;Test for user's FmtString
315                 beq     CLIOpts2
316                 move.l  #FmtString2,d2          ;Print audio ID in decimal
318 CLIOpts2:       tst.l   OptLFormat
319                 beq     PrintResult
320                 move.l  OptLFormat,d2           ;Print audio ID in hex
322 PrintResult:    move.l  _stdout,d1
323                 move.l  a1,d3
324                 jlib    Dos,VFPrintf            ;Print chosen mode
326                 tst.l   OptLFormat
327                 beq     ShutDown
328                 lea.l   CRLFText1,a0
329                 bsr     Printer                 ;The user string needs a CRLF pair
331                 bra     ShutDown
334 ;ShutDown routine. This cleans up EVERYTHING.
336 ;We have three ways to start it:
338 ;1. ERRShutDown         - does a quick turn in Printer to print anything in a0 first
339 ;2. ERRShutDown2        - makes sure we return an error code when finished
340 ;3. ShutDown            - a normal or non-fatal shutdown
343 ERRShutDown:    bsr     Printer
344 ERRShutDown2:   move.l  #RETURN_FAIL,RetCode
346 ShutDown:       tst.l   _AHIBase
347                 beq     ShutDown7000
348                 move.l  AHIReq1,a0                      ;Allowed to be NULL
349                 jlib    AHI,AHI_FreeAudioRequest
351 ShutDown7000:   tst.l   CONFile
352                 beq     ShutDown6000
353                 move.l  CONFile,d1
354                 jlib    Dos,Close
356 ShutDown6000:   move.l  RDArgs1,d1
357                 beq     ShutDown5000
358                 jlib    Dos,FreeArgs
360 ShutDown5000:   tst.l   _DosBase
361                 beq     ShutDown4000
362                 move.l  OrigLock,d1                     ;Can be zero for root
363                 jlib    Dos,CurrentDir
365 ShutDown4000:   tst.l   DiskObject
366                 beq     ShutDown1500
367                 move.l  DiskObject,a0
368                 jlib    Icon,FreeDiskObject
370 ShutDown1500:   tst.l   _LocaleBase
371                 beq     ShutDown1000
372                 move.l  GAICatalog,a0
373                 jlib    Locale,CloseCatalog
375 ShutDown1000:   CLOSEAHI
376                 closlib Locale
377                 closlib Icon
378                 closlib Dos
380                 move.l  WBMsg,d2
381                 beq     ShutDown100
382                 jlib    Exec,Forbid
383                 move.l  d2,a1
384                 flib    Exec,ReplyMsg
386 ShutDown100:    pull    all
387                 move.l  RetCode,d0
388                 rts
391 ;Subroutines
394 ;CheckLegality does a simple check on the user provided formatting string
395 ;so that we hopefully can't cause any Enforcer hits or any other nasty
396 ;things
398 ;INPUTS D2 = Pointer to formatting string
400 ;OUTPUT D2 = NULL for error or original string
402 CheckLegality:  move.l  d2,a0
403                 clr.b   d1                      ;'%' counter
405 CheckLegality2: tst.b   (a0)
406                 beq     CheckLeg_OUT            ;Do until NULL
407                 cmp.b   #'%',(a0)+
408                 bne     CheckLegality2
409                 cmp.b   #'%',(a0)               ;Let user print %-sign
410                 bne     CheckLegality3
411                 add.l   #1,a0
412                 bra     CheckLegality2
413 CheckLegality3: add.b   #1,d1
414                 cmp.b   #3,d1
415                 bne     CheckLegality2          ;OK if 2 or under
416 CheckLegality4: clr.l   d2                      ;Illegal
417 CheckLeg_OUT:   rts
420 ;Error handling routines
422 NoDos:          pull    all
423                 move.l  #RETURN_FAIL,d0
424                 rts
426 CheckCancel:    move.l  #RETURN_WARN,RetCode    ;Set return code to WARN
427 NoRDArgs:       jlib    Dos,IoErr
428                 move.l  d0,d1
429                 beq     ShutDown                ;User cancelled
430                 moveq.l #0,d2
431                 flib    Dos,PrintFault
432                 bra     ERRShutDown2
434 NoAHI:          move.l  STRNoAHIText1,a0
435                 bra     ERRShutDown
437 NoIcon:         move.l  STRNoIconText1,a0
438                 bra     ERRShutDown
440 NoAHIReq:       move.l  STRNoAHIReqText1,a0
441                 bra     ERRShutDown
443 BadFmt:         move.l  STRBadFormatText1,a0
444                 bra     ERRShutDown
447 ;Normal print function with Enforcer hit checks
449 Printer:        cmp.l   #0,a0
450                 beq     Printer_OUT             ;No string
451                 tst.b   (a0)
452                 beq     Printer_OUT             ;No content
453                 move.l  _stdout,d1
454                 move.l  a0,d2
455                 jlib    Dos,FPuts               ;Print buffered
456 Printer_OUT:    rts
459 ;Reservations start here
461                 libnames                        ;This contains macros for the library names
463                 ds.l    0
466 ;CLI/Shell stuff
468 RDArgs1:        dc.l    0                       ;CLI arguments
469 RetCode:        dc.l    RETURN_OK               ;Return this on exit
472 ;Options
474 CLArray1:
475 OptDecimal:     dc.l    0                       ;Print ID in decimal
476 OptTitle:       dc.l    0                       ;Window title
477 OptPositive:    dc.l    0                       ;Positive gad text 'OK'
478 OptNegative:    dc.l    0                       ;Negative gad text 'Cancel'
479 OptLFormat:     dc.l    0                       ;User defined formatting
480 OptPubScreen:   dc.l    NULLText1               ;Screen to open on
483 ;File stuff
485 CONFile:        dc.l    0                       ;Actuall a CON window
486 _stdout:        dc.l    0                       ;CLI or 'CON:'
487 OrigLock:       dc.l    0                       ;Directory where we started
490 ;Workbench stuff
492 WBMsg:          dc.l    0                       ;WB message
493 DiskObject:     dc.l    0                       ;Icon
496 ;AHI stuff
498 _AHIBase:       dc.l    0                       ;AHI device base
499 AHIReq1:        dc.l    0                       ;AudioID requester
501 VFPrintfArgv:   dc.l    0,0                     ;AHIAudioID, AHIMixFreq
504 ;Locale stuff
506 GAICatalog:     dc.l    0                       ;Our translation file
507 Locale1:        dc.l    0                       ;Locale we're using
510 ;Text strings
512 CLTemplate1:    dc.b    "DEC=DECIMAL/S,TITLE/K,POSITIVE/K,NEGATIVE/K,"
513                 dc.b    "LFORMAT/K,PUBSCREEN/K",0
514 DECIMALText1:   dc.b    "DECIMAL",0
515 TITLEText1:     dc.b    "TITLE",0
516 POSITIVEText1:  dc.b    "POSITIVE",0
517 NEGATIVEText1:  dc.b    "NEGATIVE",0
518 LFORMATText1:   dc.b    "LFORMAT",0
519 PUBSCREENText1: dc.b    "PUBSCREEN",0
520 CONName:        dc.b    "CON:0/10/500/70/GetAudioID/CLOSE/AUTO/WAIT",0
521 Version:        dc.b    "$VER: GetAudioID "
522                 PROGVERSION
523                 dc.b    " (c) Copyright Tomi Blinnikka 1996",0
524 FmtString1:     dc.b    "AUDIOID=0x%08lx MIXFREQ=%ld\r\n",0
525 FmtString2:     dc.b    "AUDIOID=%ld MIXFREQ=%ld\r\n",0
526 CRLFText1:      dc.b    "\r\n"          ;Continues on next line!
527 NULLText1:      dc.b    "",0
530 ;Locale strings
532 EnglishText1:   dc.b    "english",0
533 CatalogName1:   dc.b    "GetAudioID.catalog",0
536 ;Taglists
538                 ds.l    0               ;Long align
540 ;Tags for requester allocation
542 AHIReqTags1:    dc.l    AHIR_PubScreenName
543 ReqPubScr:      dc.l    0
544                 dc.l    TAG_DONE
547 ;Tags for showing requester
549 AHIReqTags2:    dc.l    AHIR_TitleText
550 ReqTitle:       dc.l    0
551                 dc.l    AHIR_PositiveText
552 ReqPos:         dc.l    0
553                 dc.l    AHIR_NegativeText
554 ReqNeg:         dc.l    0
555                 dc.l    AHIR_DoMixFreq,TRUE                     ;Have slider for MixFrequency
556                 dc.l    AHIR_InitialMixFreq,AHI_DEFMIXFREQ      ;Start with this MixFreq
557                 dc.l    TAG_DONE
560 ;Locale tags
562 CatalogTags:    dc.l    OC_Language,0
563                 dc.l    OC_BuiltInLanguage,EnglishText1 ;We have this language built-in
564                 dc.l    TAG_DONE
567 ;Locale text strings
569                 INCDIR  "LWF:AHI/GetAudioID/"           ;Change to your path
570                 INCLUDE "GAI_locale.i"
572                 END